//============================================================
// Producnt name:		Wz
// Version: 			1.0
// Coded by:			Wz (13066628480@163.com)
// Auto generated at: 	2016/10/9 10:22:04
//============================================================

using System;
using System.Collections.Generic;
using System.Text;
using Cloud.Model;
using System.Data.SqlClient;
using System.Data;
namespace Cloud.DAL
{
	public static class SourcesService
	{
		#region SqlCommandText
		private const string Save_CommandText = "insert into Sources values(@name,@item1,@item2)";
		private const string Update_CommandText = "Update Sources set name = @name,item1 = @item1,item2 = @item2 where id = @id";
		private const string Delete_CommandText = "delete from Sources where id = @id";
		private const string FindAll_CommandText = "select * from Sources";
		private const string FindById_CommandText = "select * from Sources where id=@id";
		#endregion
		
		#region ??
		
		#endregion
		
		#region ??
		
		#endregion
		
		#region Save
		public static int Save(Sources sources)
		{
			SqlParameter[] pars = new SqlParameter[]{
				new SqlParameter("@name",SqlDbType.VarChar),
				new SqlParameter("@item1",SqlDbType.Int),
				new SqlParameter("@item2",SqlDbType.VarChar)
				};
			pars[0].Value=sources.Name;
			pars[1].Value=sources.Item1;
			pars[2].Value=sources.Item2;
	
			return DBHelper.ExecuteNonQuery(Save_CommandText,CommandType.Text,pars);
		}
		#endregion
		
		#region Update
		public static int Update(Sources sources)
		{
			SqlParameter[] pars = new SqlParameter[]{
				new SqlParameter("@id",SqlDbType.Int),
				new SqlParameter("@name",SqlDbType.VarChar),
				new SqlParameter("@item1",SqlDbType.Int),
				new SqlParameter("@item2",SqlDbType.VarChar)
				};
			pars[0].Value=sources.Id;
			pars[1].Value=sources.Name;
			pars[2].Value=sources.Item1;
			pars[3].Value=sources.Item2;
	
			return DBHelper.ExecuteNonQuery(Update_CommandText,CommandType.Text,pars);
		}
		#endregion
		
		#region Delete
		public static int Delete(Sources sources)
		{
			SqlParameter[] pars = new SqlParameter[]{
				new SqlParameter("@id",SqlDbType.Int)
				};
			pars[0].Value=sources.Id;
	
			return DBHelper.ExecuteNonQuery(Delete_CommandText,CommandType.Text,pars);
		}
		#endregion
		
		#region FindAll
		public static IList<Sources> FindAll()
		{
			return CommonQuery(FindAll_CommandText,CommandType.Text,null);
		}
		#endregion
		
		#region FindById
		public static Sources FindById(Int32 id)
		{
			SqlParameter[] pars = new SqlParameter[]{
				new SqlParameter("@id",SqlDbType.Int)
			};	
			pars[0].Value = id;
			IList<Sources> list = CommonQuery(FindById_CommandText,CommandType.Text,pars);
			return list.Count > 0 ? list[0] : null;
		}
		#endregion
		
		#region ??
		
		#endregion
		
		#region ??
		
		#endregion
		
		#region CommonQuery
		private static IList<Sources> CommonQuery(string commandText, CommandType cmdType, SqlParameter[] pars)
		{
			IList<Sources> list = new List<Sources>();
			Sources sources = null;
			
			using (SqlDataReader reader = DBHelper.ExecuteQuery(commandText,cmdType,pars))
           	{
				while(reader.Read())
				{
					sources = new Sources();
                    if (reader["id"]==null)
                    {
                        sources.Id = 0;
                    }
                    else
                    {
                        sources.Id = Convert.ToInt32(reader["id"]);
                    }

                    if (reader["name"]==null)
                    {
                        sources.Name = "";
                    }
                    else
                    {
                        sources.Name = Convert.ToString(reader["name"]);
                    }

                    if (reader["item1"]==null)
                    {
                        sources.Item1 = 0;
                    }
                    else
                    {
                        sources.Item1 = Convert.ToInt32(reader["item1"]);
                    }

                    if (reader["item2"]==null)
                    {
                        sources.Item2 = "";
                    }
                    else
                    {
                        sources.Item2 = Convert.ToString(reader["item2"]);
                    }
					
					
					list.Add(sources);
				}
            }
            return list;
		}
		#endregion
		
		#region ??
		
		#endregion
		
		#region ??
		
		#endregion
	}
}



